home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / HDX_BACK / HDX302.ST / GETLN.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  2.4 KB  |  132 lines

  1. /* getln.c */
  2.  
  3. #include "osbind.h"
  4. #include "mydefs.h"
  5.  
  6. #define    QUANTUM        512L        /* #bytes to eat at a time */
  7. #define    LNBUFSIZ    (QUANTUM*2)
  8.  
  9.  
  10. static char getlnbuf[LNBUFSIZ];        /* buffer */
  11. int getlnind;                /* current spot in buffer */
  12. int getlncnt = 0;            /* #chars in buffer */
  13.  
  14.  
  15. /*
  16.  * Get line from file into buffer,
  17.  * return NULL or ptr to beginning of line.
  18.  *
  19.  * If `fd' is -1, reset the line buffer.
  20.  *
  21.  */
  22. char *getln(fd)
  23. int fd;
  24. {
  25.     register int i;
  26.     register char *p, *d;
  27.     int readamt = -1;            /* 0 if last read() yeilded 0 bytes */
  28.  
  29.     if (fd == -1)
  30.     {
  31.     getlncnt = 0;            /* initialize, return NULL */
  32.     return NULL;
  33.     }
  34.  
  35.     if (getlncnt == 0)
  36.     getlnind = 0;
  37.  
  38.   for (;;)
  39.   {
  40.     /*
  41.      * Scan for next end-of-line;
  42.      * (\r or \n, eat \n after \r).
  43.      */
  44.     for (i = 0, p = &getlnbuf[getlnind]; i < getlncnt; ++i, ++p)
  45.     if (*p == '\r' ||
  46.         *p == '\n')
  47.     {
  48.         if (*p == '\r' &&
  49.         (i+1) < getlncnt && p[1] == '\n')
  50.             ++i;
  51.  
  52.         *p = '\0';
  53.         p = &getlnbuf[getlnind];
  54.         ++i;
  55.         getlnind += i;
  56.         getlncnt -= i;
  57.         return p;
  58.     }
  59.  
  60.     /*
  61.      * Handle hanging lines by ignoring them. (*sigh*)
  62.      * (input file is exhausted, no \r or \n on last line)
  63.      */
  64.     if (!readamt && getlncnt)
  65.     {
  66.     getlncnt = 0;
  67.     *p = '\0';
  68.     return NULL;
  69.     }
  70.  
  71.     /*
  72.      * Truncate and return absurdly long lines.
  73.      */
  74.     if (getlncnt >= QUANTUM)
  75.     {
  76.     getlnbuf[getlnind + getlncnt - 1] = '\0';
  77.     getlncnt = 0;
  78.     return &getlnbuf[getlnind];
  79.     }
  80.  
  81.  
  82.     /*
  83.      * Relocate what's left of a line to the beginning
  84.      * of the buffer, and read some more of the file in;
  85.      * return NULL if the buffer's empty and on EOF.
  86.      */
  87.     if (getlnind != 0)
  88.     {
  89.     p = &getlnbuf[getlnind];
  90.     d = &getlnbuf[0];
  91.     for (i = 0; i < getlncnt; ++i)
  92.         *d++ = *p++;
  93.     getlnind = 0;
  94.     }
  95.  
  96.     if ((readamt = Fread(fd, QUANTUM, &getlnbuf[getlnind + getlncnt])) < 0)
  97.     return NULL;
  98.     getlncnt += readamt;
  99.  
  100.     if (getlncnt == 0)
  101.     return NULL;
  102.   }
  103. }
  104.  
  105.  
  106. /*
  107.  * Get '\'-continued line from a file
  108.  * into a buffer;
  109.  * return 0 on EOF, 1 on successful getLine.
  110.  *
  111.  */
  112. int cgetln(fd, buf)
  113. int fd;
  114. char *buf;
  115. {
  116.     char *b, *s;
  117.  
  118.     b = buf;
  119.     while ((s = getln(fd)) != NULL)
  120.     {
  121.     while (*s)
  122.         *b++ = *s++;
  123.     if (s[-1] != '\\')
  124.         break;        
  125.     }
  126.  
  127.     *b = '\0';
  128.     if (b != buf ||
  129.     s != NULL) return 1;
  130.     return 0;
  131. }
  132.